package com.feng.util;

import cn.hutool.core.util.StrUtil;
import com.feng.vo.PageVo;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: 丰
 * @date: 2022/12/30 11:46
 */
public final class StringUtil extends StrUtil {
    private StringUtil() {
    }

    /**
     * 如果为空就转为null
     */
    public static String emptyToNull(String str){
        if ("".equals(str)){
            return null;
        }
        return str;
    }

    public static PageVo checkPageVo(PageVo pageVo){
        // 如果为空，就设置为null
        pageVo.setStart(StringUtil.emptyToNull(pageVo.getStart()));
        pageVo.setEnd(StringUtil.emptyToNull(pageVo.getEnd()));
        pageVo.setSearch(StringUtil.emptyToNull(pageVo.getSearch()));
        return pageVo;
    }

    public static int[] integersToInts(Integer[] integers){
        int[] ints = new int[integers.length];
        for (int i = 0; i < integers.length; i++) {
            ints[i] = integers[i];
        }
        return ints;
    }

    public static String getStringDate(){
        return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
    }

    public static String getIp(){
        String ip = "127.0.0.1";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }
}
